package com.hzdracom.epubreader.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.shwread.android.qysw10000136.R;
import com.shwread.android.ui.widget.ReadingPopWindow;


public class ReadingBg extends ReadingPopWindow implements OnItemClickListener {
    private Context context;
    private GridView gridView;
    private TypedArray colors;
    private ReadingBgListener listener;

    public static ReadingPopWindow getInstance(Context context) {
        if (instance == null) instance = new ReadingBg(context);
        return instance;
    }

    private static ReadingPopWindow instance;

    private ReadingBg(Context context) {
        this.context = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.menu_pop_bg, null);
        init(context, contentView);
    }

    @SuppressLint("Recycle")
    @Override
    protected void initView(View parentView) {
        gridView = (GridView) parentView.findViewById(R.id.menu_pop_gl);
        BgAdapter adapter = new BgAdapter();
        gridView.setAdapter(adapter);
        colors = context.getResources().obtainTypedArray(R.array.epub3_reading_bg_array);
    }

    @Override
    protected void addListener() {
        gridView.setOnItemClickListener(this);
    }

    public void setListener(ReadingBgListener listener) {
        this.listener = listener;
    }

    private class BgAdapter
            extends
            BaseAdapter {

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.epub3_bg_item, null);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.epub_bg_item_tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textView.setBackgroundColor(colors.getColor(position, position));
            return convertView;
        }

        class ViewHolder {
            TextView textView;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        {
            //			Log.i("ywq", "onItemClick");
            //Toast.makeText(context, "onItemClick", Toast.LENGTH_LONG).show();
            listener.setBg(colors.getColor(position, position));
        }
    }

    @SuppressLint("InlinedApi")
    public void show(View viewParent) {
        popupWindow.setFocusable(true);

        final int[] location = new int[2];
        viewParent.getLocationOnScreen(location);
        //		Toast.makeText(context, "height="+popupWindow.getHeight(), Toast.LENGTH_LONG).show();
//		popupWindow.showAtLocation(viewParent, Gravity.NO_GRAVITY, 0, location[1]/* - 100*/);
        popupWindow.showAtLocation(viewParent, Gravity.START, 0, location[1]);
    }

    public interface ReadingBgListener {
        void setBg(int color);
    }
}
